package com.ktwlsoft.archivemanage.domain.business.repositoryService;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ktwlsoft.archivemanage.domain.dao.entity.Report;
import com.ktwlsoft.archivemanage.domain.dao.entity.ReportDetail;
import com.ktwlsoft.archivemanage.domain.dao.repository.IReportMapper;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.annualReport.AnnualReportQueryRequest;
import com.ktwlsoft.archivemanage.payload.annualReport.ReportResponse;
import com.ktwlsoft.archivemanage.service.utils.DateUtils;
import lombok.Getter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.stereotype.Service;

import javax.validation.constraints.Size;
import java.beans.BeanDescriptor;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.time.Year;
import java.util.*;

/**
 * 年度报送数据层服务类
 * @author chenkun
 * @version 1.0
 * @date 2019-11-01
 */
@Service
public class AnnualReportRepositoryService {

    @Getter
    private IReportMapper iReportMapper;

    public AnnualReportRepositoryService(IReportMapper iReportMapper) {
        this.iReportMapper = iReportMapper;
    }

    /**
     * 分页查询年度报送列表
     * @param request
     * @return PageInfo<Report>
     */
    public Result<?> findPagedList(AnnualReportQueryRequest request){
        if(request.getPn() == null){
            request.setPn(1);
        }
        if(request.getPageSize() == null){
            request.setPageSize(30);
        }
        JSONObject data = new JSONObject();
        data.put("state",request.getState() != null?request.getState():null);
        data.put("writterId",request.getWritterId() != null?request.getWritterId():null);
        data.put("writeDate",request.getWriteDate() != null?request.getWriteDate():null);
        data.put("beginDate",request.getBeginDate() != null?DateUtils.dayMinTime(request.getBeginDate()):null);
        data.put("endDate",request.getEndDate() != null?DateUtils.dayMaxTime(request.getEndDate()):null);
        data.put("unitName",request.getUnitName() != null?request.getUnitName():null);
        data.put("field",request.getField() != null?request.getField().replaceAll("[A-Z]", "_$0").toLowerCase():null);
        data.put("order",request.getOrder() != null?("ascend").equals(request.getOrder())?"ASC":"DESC":null);
        PageHelper.startPage(request.getPn(),request.getPageSize());
        List<ReportDetail> list=iReportMapper.findAnnualReportList(data);
        PageInfo<ReportDetail> pageInfo = new PageInfo<ReportDetail>(list);
        return Result.ok(pageInfo);
    }

    /**
     * 查询年度报送列表
     * @param data
     * @return List<ReportDetail>
     */
    public List<ReportDetail> findAnnualReportList(Map<String,Object> data){
        return iReportMapper.findAnnualReportList(data);
    }

    /**
     * 查询已有年度报送列表
     * @return List<ReportDetail>
     */
    public List<ReportResponse> findExistedReportList(){
        Map<String,Object> data = new HashMap<>();
        return iReportMapper.findExistedReportList(data);
    }
    /**
     * 查询年度报送列表
     * @param data
     * @return List<Map<String,Object>>
     */
    public List<Map<String,Object>> fetchAnnualReportList(Map<String,Object> data){
        return iReportMapper.fetchAnnualReportList(data);
    }
    /**
     * 根据单号查询年度报送数据
     * @param id
     * @return ReportDetail
     */
    public ReportDetail findAnnualReportInfo(Long id){
        return iReportMapper.findAnnualReportInfo(id);
    }
    /**
     * 查询年度报送信息
     * @param report
     * @return ReportDetail
     */
    public ReportDetail findAnnualReportDetail(Report report){
        return iReportMapper.findAnnualReportDetail(report);
    }
    /**
     * 添加年度报送数据
     * @param report
     * @return ReportDetail
     */
    public int addAnnualReportInfo(Report report){
        return iReportMapper.addAnnualReportInfo(report);
    }
    /**
     * 更新年度报送数据验证状态
     * @param report
     * @return int
     */
    public int updateAnnualReportInfo(Report report){
        return iReportMapper.updateAnnualReportInfo(report);
    }

    public int setTransfered(Long businessId, @Size(max = 30, message = "用户账号(不可重复)参数超长") String realName, Long userId, String transferRemark, Date date) {
        return iReportMapper.setTransfered(businessId, realName, userId, date, transferRemark);
    }

    public int cancelTransfer(Long businessId, String realName, Long userId, String transferRemark, Date date) {
        return iReportMapper.cancelTransfered(businessId, realName, userId, date, transferRemark);
    }

    public Report findAnnualReportDetailById(Long businessId) {
        return iReportMapper.getById(businessId);
    }

    public boolean existByUnitIdYear(Long unitId, Year writeDate) {
        return iReportMapper.existByUnitIdYear(unitId, writeDate) != null;
    }

    public int setInvalidByUnitIdAndYear(Long unitId, Year writeDate) {
        return iReportMapper.setInvalidByUnitIdAndYear(unitId, writeDate, false);
    }

    public int setValideByUnitIdAndYear(Long unitId, Year year) {

        return iReportMapper.setInvalidByUnitIdAndYear(unitId, year, true);
    }

    public int setCompareConfirm(Long unitId, Year year, boolean compare) {
        return iReportMapper.setCompareConfirm(unitId, year, compare);
    }
}
